package cn.edu.neusoft.food729.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Toast;

import com.example.retroiftdemo_729.R;

import java.util.List;

import cn.edu.neusoft.food729.Bean.OrdersBean;
import cn.edu.neusoft.food729.adapter.OrdersAdapter;
import cn.edu.neusoft.food729.listener.OrdersListener;
import cn.edu.neusoft.food729.model.OrdersModel;


public class getAllOrdersByUserActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;
    private OrdersAdapter myAdapter;
    private int page = 1;
    List<OrdersBean> list;

    Context context;

    private SharedPreferences sp;//简单存储
    private String sessionID = "";

    private OrdersListener listener = new OrdersListener() {
        @Override
        public void onResponse(List<OrdersBean> beanlist) {
            if (page == 1) {
                list = beanlist;
            } else {
                list.removeAll(beanlist);
                list.addAll(beanlist);
            }
            myAdapter.setList(list);
            recyclerView.setAdapter(myAdapter);
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getAllOrdersByUserActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_all_orders_by_user);

        init();


        OrdersModel model = new OrdersModel();
        context = getAllOrdersByUserActivity.this;
        sp = context.getSharedPreferences("login", MODE_PRIVATE);
        readSP();
        model.getOrdersResult(1, listener);
    }

    private void readSP() {
        sessionID = sp.getString("session", null);
    }

    private void init() {
        recyclerView = findViewById(R.id.wddd);

        layoutManager = new LinearLayoutManager(this);
        //准备数据
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        myAdapter = new OrdersAdapter(this);
        recyclerView.setAdapter(myAdapter);
        setTitle("我的订单");
    }

}